define(['jquery', 'bootstrap', 'backend', 'table', 'form','editable'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'jeek/product/index' + location.search,
                    add_url: 'jeek/product/add',
                    edit_url: 'jeek/product/edit',
                    del_url: 'jeek/product/del',
                    multi_url: 'jeek/product/multi',
                    import_url: 'jeek/product/import',
                    table: 'jeek_product',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                fixedColumns: true,
                fixedRightNumber: 1,
                //可以控制是否默认显示搜索单表,false则隐藏,默认为false
                searchFormVisible: true,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), operate: false,},
                        {field: 'category.name', title: __('分类'), searchList:$.getJSON("jeek/product/searchlist")},
                        {field: 'title', title: __('Title'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                         {field: 'price', title: __('Price'), operate: false,editable: true},
                         {field: 'description', title: __('Description'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'image', title: __('Image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1')}, formatter: Table.api.formatter.status},
                        {field: 'weigh', title: __('Weigh'), operate: false, operate:'BETWEEN',editable: true},
                        {
                            field: 'is_hidden',
                            title: __('状态'),
                            align: 'center',
                            formatter: function (value, row) {
                             // 手动生成带有data-url属性的按钮
                             var url = 'jeek/product/toggle_hide';
                             var id = row.id;
                             var title = value === 1 ? __('Hide') : __('Show');
                             var icon = value === 1 ? 'fa fa-eye-slash' : 'fa fa-eye';
                             var className = value === 1 ? 'btn btn-xs btn-danger' : 'btn btn-xs btn-success';
                             return '<a href="javascript:;" class="toggle ' + className + '" data-id="' + id + '" data-url="' + url + '" data-toggle="tooltip" data-original-title="' + title + '"><i class="' + icon + '"></i></a>';
                         },
                         events: {
                            'click .toggle': function (e, value, row) {
                                e.stopPropagation();
                                var url = $(this).data('url');
                                var id = $(this).data('id');
                                $.post(url, {id: id}, function (res) {
                                    if (res.code === 1) {
                                        // Layer.msg(res.msg, {icon: 1}); // 显示成功提示
                                        // Table.api.refresh(); // 刷新表格
                                        table.bootstrapTable('refresh');
                                    } else {
                                        Layer.msg(res.msg, {icon: 2}); // 显示错误提示
                                    }
                                });
                            }
                        }
                    },
                        // {field: 'category.name', title: __('分类'), operate: 'LIKE', table: table, formatter: Table.api.formatter.selectlist, 
                        //     events: {
                        //         'change.bs.select': function (e, row, index) {
                        //             // 处理分类选择变化
                        //         }
                        //     },
                        //     options: {
                        //         data: function () {
                        //             // 获取分类数据
                        //             return Controller.api.getCategoryTree();
                        //         },
                        //         tree: true, // 启用树形结构
                        //         id: 'id',
                        //         name: 'name',
                        //         parent_id: 'pid'
                        //     }
                        // },
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,
                            buttons: [ 
                                // {
                                //     name: 'hide',
                                //     text: __('隐藏'),
                                //     title: __('隐藏'),
                                //     classname: 'btn btn-xs btn-warning btn-magic btn-ajax',
                                //     icon: 'fa fa-eye-slash',
                                //     confirm: '确认隐藏选中的产品吗？',
                                //     url: 'jeek/product/hide',
                                //     success: function (data, ret) {
                                //         Layer.alert(ret.msg );
                                //         $("#table").bootstrapTable('refresh');
                                //     },
                                //     error: function (data, ret) {
                                //         console.log(data, ret);
                                //         Layer.alert(ret.msg);
                                //         return false;
                                //     }
                                // },
                                {
                                    name: 'copy',
                                    text: __('复制'),
                                    title: __('复制'),
                                    classname: 'btn btn-xs btn-success btn-magic btn-ajax',
                                    icon: 'fa fa-magic',
                                    confirm: '确认复制这个产品么？',
                                    url: 'jeek/product/copy',
                                    success: function (data, ret) {
                                        Layer.alert(ret.msg );
                                        $("#table").bootstrapTable('refresh');
                                        //如果需要阻止成功提示，则必须使用return false;
                                        //return false;
                                    },
                                    error: function (data, ret) {
                                        console.log(data, ret);
                                        Layer.alert(ret.msg);
                                        return false;
                                    }
                                },
                                {
                                    name: 'taocprice',
                                    text: __('规格'),
                                    title: __('规格'),
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    icon: 'fa fa-angellist',
                                    url: 'jeek/products/price/index',
                                    // url: function (row, column) { //row 表格接收到的数据
                                    //     return "pro_pricejd/index/lineid/" + row.id; //弹窗的对应后台控制器方法 这里是默认index方法 加id参数
                                    // },
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        // if(Config.admin.group_id !==3){
                                            return true;
                                        // }


                                    }
                                },
                            
                            ],

                            formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            getCategoryTree: function () {
                return $.ajax({
                    url: 'jeek/product/searchlist',
                    type: 'GET',
                    dataType: 'json',
                    async: false
                }).responseJSON;
            }
        }
    };

    return Controller;
});
